<?php
if (! defined ( 'BASEPATH' ))
	exit ( 'No direct script access allowed' );

/**
 * 存储空间
 * 
 * @author xuelange
 *        
 */
class Space extends MY_Controller {
	public function __construct() {
		parent::__construct ();
		$this->load->service("user_info_service");
	}
	public function index() {
		$this->db->select ( "id,used_space,capacity" );
		$user = $this->user_info_service->get_space($this->uid);
		if (! $user) {
			common::prf_json ( false, NULL, null, time (), TRUE, "user is not exist" );
		}
		//var_dump($user);exit;
		$ret ['percent'] = sprintf("%f",$user['used_space']/$user ['capacity']);
		$ret ['used_space'] = self::format($user ['used_space']);
		$ret ['capacity'] = self::format($user ['capacity']);

		common::prf_json ( TRUE, $ret );
		return true;
	}

	private function format($byte) {
		$b=0;
		$unit = "B";
		//小于1K
		if($byte<1024)
		{
			$b = $byte;
			$unit = "B";
		}
		//小于1MB
		else if ($byte < 1048576)
		{
			$b = round(sprintf("%f",$byte / 1024),2);
			$unit = "KB";
		}
		else if ($byte < 1073741824)
		{
			$b = round(sprintf("%f",$byte / 1048576),2);
			$unit = "MB";
		}
		else if($byte < 1099511627776)
		{
			$b = round(sprintf("%f",$byte/1073741824),2);
			$unit = "GB";
		}
		else
		{
			$b = round(sprintf("%f",$byte/1099511627776),2);
			$unit = "TB";
		}

		$b .= $unit;
		return $b;
	}
}